<template>
	<view class="container">
		<view class="secure" @click="handleOpenPage(5)"> <text>账号安全</text>
			<view>
				<uni-icons type="forward" color="#262626" size="20"></uni-icons>
			</view>
		</view>
		<!-- 主体部分 -->
		<view class="catalog">
			<view>
				<text>推送消息</text>
				<switch :checked="checked0" class="switch" @change="handleChange($event, 0)" color="#FFCC33" />
			</view>
			<!-- <view>
				<text>语音播报</text>
				<switch :checked="checked1" class="switch" @change="handleChange($event, 1)" color="#FFCC33" />
			</view> -->
			<view v-if="userType==0">
				<text>接受邀请</text>
				<switch :checked="checked2" class="switch" @change="handleChange($event, 2)" color="#FFCC33" />
			</view>
			<view v-if="userType==1" style="position: relative;">
				<view style="position: absolute;left:18%;top:9%">
					<uni-tooltip :styleType="5" :styleType2="1" :styleType3="8"
						content="开工审核开启后，雇员需向您申请开工，待您同意后雇员才可以开始工作">
						<image style="width: 54rpx;height: 100%;" src="../../static/helpwen.png" mode="widthFix">
						</image>
					</uni-tooltip>
				</view>
				<text>开工审核</text>
				<switch :checked="checked3" class="switch" @change="handleChange($event, 3)" color="#FFCC33" />
			</view>
			<!-- <view @click="handleOpenPage(4)">
				<text>切换身份</text>
				<view>
					<text style="color: rgba(64, 33, 16, 0.5);">转雇主</text>
					
				</view>
			</view> -->
		</view>
		<view class="catalog">
			<view @click="handleClearCache">
				<text>清除缓存</text>
				<view>
					<view>{{ storageSize }}M</view>
					<uni-icons type="forward" color="#262626" size="20"></uni-icons>
				</view>
			</view>
			<!-- <view @click="handleOpenPage(0)">
				<text>意见反馈</text>
				<view>
					<uni-icons type="forward" color="#262626" size="20"></uni-icons>
				</view>
			</view> -->
			<view @click="handleOpenPage(1)">
				<text>关于我们</text>
				<view>
					<uni-icons type="forward" color="#262626" size="20"></uni-icons>
				</view>
			</view>
			<view @click="handleOpenPage(2)">
				<text>{{ detailService }}</text>
				<view>
					<uni-icons type="forward" color="#262626" size="20"></uni-icons>
				</view>
			</view>
			<!-- <view @click="cancellation">
				<text>账户注销</text>
				<view>
					<image src="/static/icon1/17.png" mode="aspectFill"></image>
				</view>
			</view> -->
			<view @click="handleOpenPage(3)">
				<text>{{detailPrivacyT}}</text>
				<view>
					<uni-icons type="forward" color="#262626" size="20"></uni-icons>
				</view>
			</view>
			<view>
				<text>版本号</text>
				<view>
					<text>{{version}}</text>
					<!-- <image src="/static/icon1/17.png" mode="aspectFill"></image> -->
				</view>
			</view>
		</view>
		<view class="catalog" @click="handleSwitchUserType">
			<view>
				<text>切换身份</text>
				<view>
					<view>转雇主</view>
					<uni-icons type="forward" color="#262626" size="20"></uni-icons>
				</view>
			</view>
		</view>
		<view class="button" @click="handleExitApp">安全退出</view>
		<hasMonery v-if="ishasMonery" @toCloseIsMonery='popClose' @openKefu='handleMakePhone'></hasMonery>
		<kefu v-if="isShowKefu" @closePage="isShowKefu=false"></kefu>
	</view>
</template>
<script>
	import kefu from '../../components/xjbg/kefu.vue'
	import hasMonery from '../my/hasMonery.vue'
	export default {
		data() {
			return {
				detailPrivacyT: '隐私政策', //隐私标题
				detailService: '服务协议', //服务
				storageSize: 0,
				userType: uni.getStorageSync('userType'),
				checked0: true, //开关是否开着
				checked1: true, //开关是否开着
				checked2: true, //开关是否开着
				checked3: false, //开关是否开着
				userInfo: {
					user: {},
					params: {}
				}, //当前用户信息
				version: '0.0.0',
				ishasMonery: false,
				isShowKefu: false
			}
		},
		components: {
			kefu,
			hasMonery
		},
		methods: {
			// 获取banner,缓存起来
			async getConfigBanner(e) {
				try {
					// if (uni.getStorageSync('banner')) {
					// 	if (uni.getStorageSync('banner').toString() != resData.toString()) {}
					// }
					// let list = uni.getStorageSync('banner')
					// let timer = false
					// if (list) {
					// 	this.configBanner = list
					// 	return
					// }
				} catch (e) {
					console.log(e, 99)
				}
				let tempRegion
				// #ifdef H5
				tempRegion = '330782'
				// #endif
				// #ifdef APP-PLUS
				tempRegion = uni.getStorageSync('region1').slice(0, 6)
				// #endif
				let res = await this.$fetch(this.$api.bannerByToken, {
					region: e ? e : tempRegion
				}, 'GET')
				if (res.code) return uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				let newList = []
				for (let i = 0; i < res.data.length; i++) {
					let resdate = res.data[i]
					resdate.img = `https://images.linglinggong.net/${resdate.img}`
					newList.push(resdate)
				}
				uni.setStorageSync('configBanner', newList)
				return
			},
			// 关闭
			popClose() {
				this.isShowPop = false
				this.ishasMonery = false
			},
			// 查看
			onOpen() {
				this.isShowPop = true
			},
			async getServiceTelephone() {
				let res = await this.$fetch(this.$api.property_list_by_type_and_key, {
					type: 1,
					key: 'telephone'
				}, 'POST')
				if (res.code) return uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				this.serviceTelephone = res.data
			},
			// 版本号   api.get_new_version
			async newVersion() {
				const platform = uni.getSystemInfoSync().platform;
				let res = await this.$fetch(this.$api.get_new_version, {
					type: platform == 'android' ? 1 : 2
				}, 'GET')
				if (res.code) return uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				this.version = JSON.parse(res.msg).appVersion.split('').join('.')
				console.log(JSON.parse(res.msg).appVersion)
			},
			handleMakePhone() {
				this.isShowKefu = true
				this.popClose()
			},
			//切换用户身份
			async handleSwitchUserType() {
				let res = await this.$fetch(this.$api.checkUserIdentity, {
					type: uni.getStorageSync('userInfo').userType
				}, 'POST')
				if (res.code) {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					return
				}
				if (this.userInfo.amount < 100) {
					uni.showModal({
						title: '请先回答问题',
						content: '',
						success: (res) => {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/switchBranch/switchBranch'
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
				} else {
					this.ishasMonery = true
				}
			},
			cancellation() {
				uni.showModal({
					title: '提示',
					content: '确定是否注销账号？',
					success: (res) => {
						if (res.confirm) {
							this.logOutUser();
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			//注销账户
			async logOutUser() {
				let res = await this.$fetch(this.$api.logOutUser, uni.getStorageSync('token'), 'POST')
				if (res.code == 0) {
					uni.removeStorageSync('userInfo');
					uni.removeStorageSync('token');
					uni.removeStorageSync('userType');
					uni.removeStorageSync('userId');
					uni.removeStorageSync('level');
					uni.removeStorageSync('first-is');
					uni.removeStorageSync('isHavePayPassword');
					uni.showToast({
						title: '账户已注销',
						icon: 'none'
					})
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/index/index'
						})
					}, 600)
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			// 获取隐私文章详情
			async getArticleDetailP() {
				let res = await this.$fetch(this.$api.get_article_by_id, {
					id: 4
				}, 'POST')
				if (res.code) return uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				this.detailPrivacyT = res.data.articleTitle
			},
			// 获取服务详情
			async getArticleDetailS() {
				let res = await this.$fetch(this.$api.get_article_by_id, {
					id: 3
				}, 'POST')
				if (res.code) return uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				this.detailService = res.data.articleTitle
				// console.log(this.detailService)
			},
			// 打开意见反馈
			handleOpenComplain() {
				uni.navigateTo({
					url: '/pages/tousu/boss/recommendation'
				})
			},
			//开关变化
			async handleChange(e, flag) {
				if (flag === 0) {
					this.checked0 = e.detail.value === 'false' || !e.detail.value ? false : true
					let pinf = plus.push.getClientInfo();
					let cidNow = pinf.clientid; //客户端标识  
					let cid = this.checked0 == 0 ? 0 : cidNow
					console.log('cid', cid)
					let res = await this.$fetch(this.$api.upd_user_detail, {
						cid: cid
					}, 'POST')
					if (res.code) return uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
				if (flag === 1) {
					console.log(e.detail.value, 777777)
					uni.setStorageSync('checked1', e.detail.value)
					console.log('播报语音设置', uni.getStorageSync('checked1'))
				}
				if (flag === 2) {
					this.checked2 = e.detail.value === 'false' || !e.detail.value ? false : true
					let isInvite = this.checked2 ? 1 : 0
					let res = await this.$fetch(this.$api.upd_user_detail, {
						isInvite: isInvite
					}, 'POST')
					if (res.code) return uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
				if (flag == 3) {
					this.checked3 = e.detail.value === 'false' || !e.detail.value ? false : true
					let res = await this.$fetch(this.$api.automatic_check_switch, {}, 'POST')
					if (res.code) return uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					// console.log('关闭开工审核')
				}
				this.getUserInfo()
			},
			//清除缓存
			handleClearCache() {
				uni.showModal({
					title: '提示',
					content: '确定清除缓存吗?',
					success: (res) => {
						// 用户确定要删除
						if (res.confirm) {
							uni.showToast({
								title: '清除成功~',
								icon: 'none'
							})
							this.storageSize = 0
							uni.setStorageSync('storageSize', 0)
							uni.removeStorageSync('banner')
						}
					}
				})
			},
			// 打开内页面
			handleOpenPage(page) {
				if (page == 0) {
					uni.navigateTo({
						url: '/pages/tousu/boss/recommendation'
					})
				} else if (page == 1) {
					uni.navigateTo({
						url: '/pages/about-us/about-us'
					})
				} else if (page == 2) {
					uni.navigateTo({
						url: '/pages/user-agreement/service-agreement'
					})
				} else if (page == 3) {
					uni.navigateTo({
						url: '/pages/user-agreement/privacy-agreement'
					})
				} else if (page == 4) {
					uni.navigateTo({
						url: '/pages/change-identity/change-identity'
					})
				} else if (page == 5) {
					uni.navigateTo({
						url: '/pages/account-security/account-security'
					})
				}
			},
			// 关闭推送
			async exitCloseCid() {
				let res = await this.$fetch(this.$api.upd_user_detail, {
					cid: 0
				}, 'POST')
				if (res.code) return uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				console.log('关闭推送', res)
				this.getUserInfo()
			},
			// 获取当前用户信息
			async getUserInfo() {
				let res = await this.$fetch(this.$api.get_current_user, {}, 'GET')
				// if (res.code) return uni.showToast({
				// 	title: res.msg,
				// 	icon: 'none'
				// })
				if (res.code === 531) {
					uni.navigateTo({
						url: '/pages/riskControl/riskControl',
					})
					return
				}
				if (res.data.payPwdFlag) {
					uni.setStorageSync('isHavePayPassword', true)
				} else {
					uni.setStorageSync('isHavePayPassword', false)
				}
				console.log('获取当前用户信息', res.data)
				uni.setStorageSync('userInfo', res.data)
				uni.setStorageSync("level", res.data.user.level) //用户等级
			},
			//安全退出
			handleExitApp() {
				// 关闭推送
				this.exitCloseCid()
				uni.showToast({
					title: '已退出~',
					icon: 'none'
				})
				try {
					uni.removeStorageSync('userInfo');
					uni.removeStorageSync('token');
					uni.removeStorageSync('userType');
					uni.removeStorageSync('userId');
					uni.removeStorageSync('level');
					uni.removeStorageSync('first-is');
					uni.removeStorageSync('isHavePayPassword');
					uni.removeStorageSync('guhzuTime')
				} catch (e) {
					// error
				}
				uni.$emit('userLogin', {
					online: 0
				})
				uni.$emit('closeWorking', {})
				uni.$emit('closeDaikgong', {})
				uni.$emit('closeFjin', {})
				// try {
				// 	uni.clearStorageSync();
				// } catch (e) {
				// 	console.log(e)
				// }
				// uni.setStorageSync('location', [])
				this.getConfigBanner()
				setTimeout(() => {
					uni.reLaunch({
						url: '/pages/index/index'
					})
					// location.reload()
				}, 600)
			},
			// 每天显示的随机缓存
			showStorage() {
				this.version = uni.getStorageSync('version')
				this.storageSize = uni.getStorageSync('storageSize') ? uni.getStorageSync('storageSize') : 0
				let x = Math.round(Math.random() * 10) / 30
				uni.setStorageSync('storageSize', this.storageSize + x)
				this.storageSize = (this.storageSize + x).toFixed(1)
			},
			// 获取当前用户信息
			async getUserInfo() {
				let res = await this.$fetch(this.$api.get_current_user, {}, 'GET')
				if (res.code) return
				// 整理数据,res.data.user为空，则改为""
				Object.getOwnPropertyNames(res.data.user).forEach(function(key) {
					res.data.user[key] = res.data.user[key] == null ? '' : res.data.user[key]
				})
				if (res.data.payPwdFlag) {
					uni.setStorageSync('isHavePayPassword', true)
				} else {
					uni.setStorageSync('isHavePayPassword', false)
				}
				uni.setStorageSync('userInfo', res.data)
				this.userInfo = res.data
				this.checked0 = this.userInfo.cid == 0 ? false : true //是否推送消息
				this.checked2 = this.userInfo.isInvite ? true : false //是否接受邀请
				this.checked3 = this.userInfo.taskAutoAudit == 0 ? false : true //是否开工审核
			},
		},
		onLoad() {
			this.getUserInfo()
			this.getArticleDetailS()
			this.getArticleDetailP()
			this.getServiceTelephone()
			// #ifdef APP-PLUS
			this.showStorage()
			this.checked1 = uni.getStorageSync('checked1')
			// uni.setStorageSync('version', (plus.runtime.version).split('.').join('.'))
			// #endif
			// console.log('版本',uni.getStorageSync('version') )
		},
		onShow() {
			this.checked1 = uni.getStorageSync('checked1')
			console.log(uni.getStorageSync('checked1'), 789)
			this.getUserInfo()
			this.newVersion()
		}
	}
</script>
<style lang="less">
	.container {
		overflow: hidden;
		font-size: 32rpx;

		.secure {
			height: 120rpx;
			line-height: 120rpx;
			background-color: #FFFFFF;
			color: #1A1A1A;
			padding: 0 30rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: space-between;

			&>text {
				font-weight: bold;
			}

			&>view {
				margin-right: 20rpx;
			}
		}

		// border: 1px solid #000000;
		.catalog {
			margin-top: 30rpx;
			background-color: #FFFFFF;

			&>view {
				height: 120rpx;
				padding: 0 30rpx;
				position: relative;
				display: flex;
				align-items: center;
				justify-content: space-between;
				box-sizing: border-box;
				border-bottom: 1px solid rgba(241, 241, 245, 0.9);

				&>text {
					color: #1A1A1A;
					font-weight: bold;
				}

				&>.switch {
					transform: scale(0.9);
					// position: absolute;
					// right: 15rpx;
				}

				&>view {
					display: flex;
					align-items: center;
					margin-right: 20rpx;

					&>view {
						margin-right: 10rpx;
						color: #1A1A1A;
						letter-spacing: 0;
					}

					&>image {
						width: 16rpx;
						height: 26rpx;
					}
				}

				// &::after {
				// 	content: "";
				// 	position: absolute;
				// 	top: 100rpx;
				// 	width: 720rpx;
				// 	height: 2rpx;
				// 	background: #E4E4E4;
				// }
			}
		}

		&>.button {
			margin: 110rpx 30rpx auto 30rpx;
			width: 690rpx;
			height: 88rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			border-radius: 12rpx;
			color: #F85A41;
			font-weight: bold;
			font-size: 36rpx;
			letter-spacing: 0;
			border: 1px solid #BDBDBD;

			&:active {
				opacity: 0.7;
			}
		}
	}
</style>